<?php
declare (strict_types = 1);

namespace app\model;


use app\constant\ModelConstant;
use think\helper\Arr;
use think\helper\Str;
use think\model\relation\HasMany;
use think\Request;

/**
 * @property string $host
 * @property string $name
 * @property string $api_key
 * @property string $api_secret
 * @property string $admin_token
 * @property string $store_token
 * @property string $version
 * @property integer $status
 * @property HasMany $payments
 * @property integer $pay_step
 * @property string $website
 * @property string $api_host
 * @property integer $is_repurchase
 * @property string $tongtu_account 通途代码
 */
class Shops extends BaseModel
{
    public $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'U';
    protected $field = [
        'host','name','api_key',
        'api_secret','admin_token','store_token',
        'version','created_at','updated_at','status',
        'pay_step','website','is_repurchase','tongtu_account'
    ];
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';
    public function payments()
    {
        return $this->hasMany(ShopsPayment::class,'shop_id');
    }

    public function scopeHost($query,$host)
    {
        return $query->where('host',$host);
    }

    public static function getEnablePayment(Request $request)
    {
        $shop = self::query()->host($request->header('X-Opc-Shop-Id'))->find();

        $payments  =  $shop->payments()
            ->status(ModelConstant::STATUS_ON)
            ->order('pay_method','DESC')
            ->select();
        if($payments->isEmpty()) return [];
        $data = [];
        $payments->each(function (ShopsPayment $payment) use (&$data) {
            $config = $payment->config;
            $cnf = [
                'payment_id'=>$payment->id,
                'pay_method'=>$payment->pay_method,
                'account'=>$payment->account,
                'mode'=>Arr::get($config,'env','live')
            ];
            if($payment->pay_method == ShopsPayment::PAY_METHOD_PAYPAL) {
                $cnf['client_id'] = Arr::get($config,'app_key');
                $cnf['merchant_id'] = Arr::get($config,'merchant_no');
            }
            $data[] = $cnf;
        });
        return $data;
    }

    public function setStatusAttr($status)
    {
        return $status == ModelConstant::STATUS_ON_NAME ? ModelConstant::STATUS_ON : ModelConstant::STATUS_OFF;
    }

    public function getStatusAttr($status)
    {
        return $status == ModelConstant::STATUS_ON ? ModelConstant::STATUS_ON_NAME : ModelConstant::STATUS_OFF_NAME;
    }

    public function getApiHostAttr()
    {
        $host = $this->getData('host');
        if(Str::contains($host,'myshopify.com')) return $host;
        return sprintf('%s.myshopify.com',$this->getData('name'));
    }

    public function getWebsiteAttr()
    {
        $website = $this->getData('website');
        if($website) return $website;
        return sprintf('%s.myshopify.com',$this->getData('name'));
    }
    
}
